var crypto = require('crypto');

var KEY = 'Af7ugPbk3UvU2WU2Y37xww27nyWak2Dj';
var IV = '';

/**
 * aes加密
 * @param data 待加密内容
 * @param KEY 必须为32位私钥
 * @returns {string}
 */
export const encryption = (data) => {
    var clearEncoding = 'utf8';
    var cipherEncoding = 'base64';
    var cipherChunks = [];
    var cipher = crypto.createCipheriv('aes-256-ecb', KEY, IV);
    cipher.setAutoPadding(true);
    cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding));
    cipherChunks.push(cipher.final(cipherEncoding));
    return encodeURIComponent(cipherChunks.join(''));
}

/**
 * aes解密
 * @param data 待解密内容
 * @param KEY 必须为32位私钥
 * @returns {string}
 */
export const decryption = (data) => {
    if (!data) return "";
    var clearEncoding = 'utf8';
    var cipherEncoding = 'base64';
    var cipherChunks = [];
    var decipher = crypto.createDecipheriv('aes-256-ecb', KEY, IV);
    decipher.setAutoPadding(true);
    cipherChunks.push(decipher.update(decodeURIComponent(data), cipherEncoding, clearEncoding));
    cipherChunks.push(decipher.final(clearEncoding));
    return cipherChunks.join('');
}

